﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UFSoft.UBF.Service.Base;

namespace UFSoft.UBF.Cust.ClassView.MVC.Proxy
{
    public static class ProxyHelper
    {
        /// <summary>
        ///     获取请求代理对象
        /// </summary>
        /// <param name="proxyType"></param>
        /// <returns></returns>
        public static ProxyBase GetProxyBaseObject(string proxyType)
        {
            Type loadType = GetType(proxyType);
            if (loadType == null)
                throw new ProxyException("proxyType is incorrect");
            return Activator.CreateInstance(loadType) as ProxyBase;
        }

        /// <summary>
        ///     获取请求代理对象
        /// </summary>
        /// <param name="proxyType"></param>
        /// <returns></returns>
        public static Type GetType(string proxyType)
        {
            if (proxyType == null)
                throw new ProxyException("proxyType is null");
            return TypeManager.TypeLoader.LoadType(proxyType);
        }

        /// <summary>
        ///     尝试加载代理
        /// </summary>
        /// <param name="bpsvFullName"></param>
        /// <param name="bpsvAssemblyName"></param>
        /// <param name="agentType"></param>
        /// <returns></returns>
        public static bool TryLoadProxy(string bpsvFullName, string bpsvAssemblyName, out Type agentType)
        {
            agentType = null;
            if (string.IsNullOrEmpty(bpsvAssemblyName) || string.IsNullOrEmpty(bpsvAssemblyName))
                return false;
            //代理类dll
            string tempProxyAssemblyName = (bpsvAssemblyName.ToLower().EndsWith(".dll")
                                               ? bpsvAssemblyName.Substring(0, bpsvAssemblyName.Length - 4)
                                               : bpsvAssemblyName)
                                           + ".Agent.dll";
            try
            {
                Assembly assembly = TypeManager.TypeLoader.ProbeAssembly(tempProxyAssemblyName);
                if (assembly == null)
                    return false;
                string[] arrFullName = bpsvFullName.Split('.');
                string proxyClassName = arrFullName[arrFullName.Length - 1] + "Proxy";
                string tempAgentTypeFullName = string.Join(".", arrFullName.Take(arrFullName.Length - 1)) + ".Proxy." +
                                               proxyClassName;
                List<Type> types = new List<Type>();
                foreach (Type searchType in assembly.GetTypes())
                {
                    if (searchType.FullName == tempAgentTypeFullName)
                    {
                        agentType = searchType;
                        break;
                    }

                    if (searchType.Name == proxyClassName && searchType.IsSubclassOf(typeof(ProxyBase)))
                        types.Add(searchType);
                }

                if (agentType == null && types.Count != 1) return false;
                if (agentType == null)
                    agentType = types[0];
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}